from flask import Blueprint, render_template, request, redirect, session, jsonify
import json
from ..utils import db

rg = Blueprint(name="register", import_name=__name__, url_prefix='/')


@rg.route("/register", methods=['GET', 'POST'])
def register():
    if request.method == 'GET':
        return render_template('register.html')

    username = request.form["user"]
    if not username:
        # 返回一个错误响应
        return redirect('/register')
    password = request.form["pwd"]
    email = request.form["email"]
    print(username, password, email)
    if username and password and email:
        db.insert_db_user(username, password, email)
    else:
        return redirect('/register')
    return redirect('/login')
